package isValid20;

import java.util.Stack;

public class IsValid {

    public static void main(String[] args) {
        IsValid isValid = new IsValid();
    }

    public boolean isValid(String s) {
        if(s.length()<2){
            return false;
        }
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i]=='(' || chars[i]=='[' || chars[i]=='{'){
                stack.push(chars[i]);
            }else{
                if(stack.empty()){
                    return false;
                }
                Character pop = stack.pop();
                //如果栈中弹出的不是之前放的左括号，并且当前遍历的是右括号，说明是不匹配
                if((chars[i] == ')' && pop != '(') || (chars[i] == ']' && pop != '[') || (chars[i] == '}' && pop != '{')){
                    return false;
                }
            }
        }
        if (stack.empty()) {
            return true;
        }
        return false;
    }

}
